package com.summo.demo.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.summo.demo.config.log.Log;
import com.summo.demo.config.permissions.RequiresPermissions;
import com.summo.demo.model.request.AddReq;
import com.summo.demo.model.request.UpdateReq;
import com.summo.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public void userLogin(@RequestParam(required = true) String userName,
        @RequestParam(required = true) String password,
        @RequestParam(required = false) String redirectURL,
        HttpServletRequest httpServletRequest,
        HttpServletResponse httpServletResponse) {
        userService.login(userName, password, redirectURL, httpServletRequest, httpServletResponse);
    }


    @GetMapping("/add")
    @RequiresPermissions(value = "add")
    @Log(methodDesc = "添加用户")
    public ResponseEntity<String> add(@RequestBody AddReq addReq) {
        return userService.add(addReq);
    }

    @GetMapping("/delete")
    @RequiresPermissions(value = "delete")
    @Log(methodDesc = "删除用户")
    public ResponseEntity<String> delete(@RequestParam Long userId) {
        return userService.delete(userId);
    }

    @GetMapping("/query")
    @RequiresPermissions(value = "query")
    public ResponseEntity<String> query(@RequestParam String userName) {
        return userService.query(userName);
    }

    @GetMapping("/update")
    @RequiresPermissions(value = "update")
    @Log(methodDesc = "更新用户")
    public ResponseEntity<String> update(@RequestBody UpdateReq updateReq) {
        return userService.update(updateReq);
    }
}
